<html>
<head><meta charset="utf-8"><title>enum repr · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html">enum repr</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="152202226"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152202226" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152202226">(Dec 19 2018 at 18:24)</a>:</h4>
<p>Finally finished my write-up of enum representation:</p>
<p><a href="https://github.com/rust-rfcs/unsafe-code-guidelines/pull/57" target="_blank" title="https://github.com/rust-rfcs/unsafe-code-guidelines/pull/57">https://github.com/rust-rfcs/unsafe-code-guidelines/pull/57</a></p>



<a name="152214859"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152214859" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> briansmith <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152214859">(Dec 19 2018 at 21:34)</a>:</h4>
<p><span class="user-mention" data-user-id="116009">@nikomatsakis</span> I was surprised to see you propose that non-C-compatible layouts are guaranteed to have the tag at the beginning, IIUC. I have several cases where it would be better for the tag to be put at the end. In particular, when the enum has a variant with large alignment, forcing the tag to be at the beginning wastes a huge amount of space (127 bits of space, in my cases).</p>



<a name="152214876"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152214876" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> briansmith <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152214876">(Dec 19 2018 at 21:34)</a>:</h4>
<p>Is it really important to guarantee the layout of these?</p>



<a name="152215042"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152215042" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152215042">(Dec 19 2018 at 21:37)</a>:</h4>
<p><span class="user-mention" data-user-id="133214">@briansmith</span> Are you talking about repr(Int) on enums whose variants have fields? That was explicitly RFC'd to do what that PR documents (<a href="https://github.com/rust-lang/rfcs/pull/2195" target="_blank" title="https://github.com/rust-lang/rfcs/pull/2195">https://github.com/rust-lang/rfcs/pull/2195</a>)</p>



<a name="152215151"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152215151" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> briansmith <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152215151">(Dec 19 2018 at 21:39)</a>:</h4>
<p><span class="user-mention" data-user-id="124289">@rkruppe</span> I'm just talking about regular enums like <code>enum E { A(A), B(B) }</code> where types A and/or B have large alignment.</p>



<a name="152215180"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152215180" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152215180">(Dec 19 2018 at 21:39)</a>:</h4>
<p>Huh, does the PR specify that? If so, I must have missed it</p>



<a name="152215253"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152215253" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthew Jasper <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152215253">(Dec 19 2018 at 21:40)</a>:</h4>
<p>How does it waste space? You still need padding at the end.</p>



<a name="152215306"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152215306" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152215306">(Dec 19 2018 at 21:41)</a>:</h4>
<p><span class="user-mention" data-user-id="133214">@briansmith</span> I believe that I explicitly said that the layout without a <code>#[repr]</code> is not defined</p>



<a name="152215315"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152215315" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152215315">(Dec 19 2018 at 21:41)</a>:</h4>
<p>if not, then I made a mistake, or perhaps I wrote it in a confusing way</p>



<a name="152215319"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152215319" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152215319">(Dec 19 2018 at 21:41)</a>:</h4>
<p>(I did document the current behavior, which might not be worth doing)</p>



<a name="152215385"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152215385" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> briansmith <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152215385">(Dec 19 2018 at 21:42)</a>:</h4>
<p>Oh, I see: I interpreted "Non-C-compatible layouts" to mean "all Non-C-compatible-layouts" but it's really only Non-C-compatible layouts that have a <code>#[repr(Int)]</code>.</p>



<a name="152215391"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152215391" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> briansmith <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152215391">(Dec 19 2018 at 21:42)</a>:</h4>
<p>I agree that everything is OK.</p>



<a name="152215430"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152215430" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> briansmith <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152215430">(Dec 19 2018 at 21:43)</a>:</h4>
<p><span class="user-mention" data-user-id="116118">@Matthew Jasper</span> Whether you need padding at the end depends on the alignment of the object that follows it. That object mighty have alignment 1</p>



<a name="152215623"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152215623" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthew Jasper <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152215623">(Dec 19 2018 at 21:46)</a>:</h4>
<p>No? In <code>struct A { f: i64, g: i8 }, struct B { a: A, h: i8}</code> B must be at least 24 bytes.</p>



<a name="152215900"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152215900" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> briansmith <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152215900">(Dec 19 2018 at 21:51)</a>:</h4>
<p><span class="user-mention" data-user-id="116118">@Matthew Jasper</span> Really? I thought that Rust reserved the right to not only pack things into padding, but also even reorder members of structs that don't have explicit <code>#[repr(...)]</code>.</p>



<a name="152216651"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152216651" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthew Jasper <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152216651">(Dec 19 2018 at 22:03)</a>:</h4>
<p>It can reorder members, but all orders are the same here. It has to guarantee that one can write to all <code>mem::size_of::&lt;A&gt;()</code> bytes of a <code>&amp;mut A</code> that refers to <code>a</code>.</p>



<a name="152217284"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152217284" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152217284">(Dec 19 2018 at 22:14)</a>:</h4>
<blockquote>
<p>Oh, I see: I interpreted "Non-C-compatible layouts" to mean "all Non-C-compatible-layouts" but it's really only Non-C-compatible layouts that have a <code>#[repr(Int)]</code>.</p>
</blockquote>
<p>I tried to make it more clear</p>



<a name="152239740"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152239740" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152239740">(Dec 20 2018 at 07:47)</a>:</h4>
<blockquote>
<p><span class="user-mention" data-user-id="116118">@Matthew Jasper</span> Really? I thought that Rust reserved the right to not only pack things into padding, but also even reorder members of structs that don't have explicit <code>#[repr(...)]</code>.</p>
</blockquote>
<p>packing into padding is impossible unfortunately; <code>mem::swap</code> would break: it overrides all bytes of both objects. so e.g. whatever <code>&amp;mut b.a</code> points to cannot overlap with anything packed</p>



<a name="152239752"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152239752" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152239752">(Dec 20 2018 at 07:47)</a>:</h4>
<p>though... I wonder if there is any chance that we could ever have a type whose size is unequal to its stride in an array?</p>



<a name="152239800"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152239800" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152239800">(Dec 20 2018 at 07:48)</a>:</h4>
<p>I see no fundamental reason why such types couldn't exist, except that tons of unsafe code could rely on such types not existing...</p>



<a name="152244226"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152244226" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152244226">(Dec 20 2018 at 09:33)</a>:</h4>
<p>Yeah, it's invalid not because it's invalid as a concept, but because it would break so much code.</p>



<a name="152244229"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152244229" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152244229">(Dec 20 2018 at 09:33)</a>:</h4>
<p>Swift does this, for example</p>



<a name="152244659"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152244659" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152244659">(Dec 20 2018 at 09:42)</a>:</h4>
<p>shame that we didn't do this from the start, to avoid such code :/</p>



<a name="152268147"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152268147" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152268147">(Dec 20 2018 at 16:31)</a>:</h4>
<p>btw <span class="user-mention" data-user-id="128323">@Alan Jeffrey</span> I can't tell how to respond to this on github but I don't quite understand this comment:</p>
<blockquote>
<p>Which indexing types do you mean? Option&lt;usize&gt; takes 128 bits according to the playground.</p>
</blockquote>
<p>This is because <code>usize</code> does not define any niche values -- all bitpatterns are valid.</p>



<a name="152268364"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152268364" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152268364">(Dec 20 2018 at 16:33)</a>:</h4>
<p>I responded on GH</p>



<a name="152268603"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152268603" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152268603">(Dec 20 2018 at 16:36)</a>:</h4>
<p><span class="user-mention" data-user-id="116009">@nikomatsakis</span> what <span class="user-mention" data-user-id="120791">@RalfJ</span> said.</p>



<a name="152268685"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152268685" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152268685">(Dec 20 2018 at 16:37)</a>:</h4>
<p>I can't figure out what <span class="user-mention" data-user-id="120791">@RalfJ</span> said</p>



<a name="152268697"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152268697" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152268697">(Dec 20 2018 at 16:37)</a>:</h4>
<p>GH reviews can be very weird</p>



<a name="152268714"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152268714" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152268714">(Dec 20 2018 at 16:37)</a>:</h4>
<p>ah, I see</p>



<a name="152268726"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152268726" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152268726">(Dec 20 2018 at 16:37)</a>:</h4>
<p>that was in the context of another thread earlier up, ok</p>



<a name="152268776"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152268776" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152268776">(Dec 20 2018 at 16:38)</a>:</h4>
<p>no wonder it seemed so random</p>



<a name="152268779"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152268779" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152268779">(Dec 20 2018 at 16:38)</a>:</h4>
<p>TIL about <code>rustc_layout_scalar_valid_range_end</code></p>



<a name="152268794"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152268794" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152268794">(Dec 20 2018 at 16:38)</a>:</h4>
<p>Do we want to say something about that in the discussion of niches?</p>



<a name="152268929"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152268929" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152268929">(Dec 20 2018 at 16:40)</a>:</h4>
<p>I have been assuming that is the next area -- i.e., when we discuss validity invariants (although I agree with you it is potentially useful to peel off a "representation invariant" or "bitstring invariant" or something)</p>



<a name="152268958"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152268958" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152268958">(Dec 20 2018 at 16:41)</a>:</h4>
<p>Can we peel off "niche", to make the representation section self-contained?</p>



<a name="152269054"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152269054" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152269054">(Dec 20 2018 at 16:42)</a>:</h4>
<p>Might make sense. I'd still be a bit reluctant to discuss <code>#[rustc_layout_scalar_valid_range_end]</code> -- well, I guess it's <em>ok</em>, it's just that it's pretty wildly unstable.</p>



<a name="152269226"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152269226" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152269226">(Dec 20 2018 at 16:44)</a>:</h4>
<p>Might be worth saying something vague then, about mechanisms for user-defined niches still being unstable.</p>



<a name="152269279"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152269279" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152269279">(Dec 20 2018 at 16:45)</a>:</h4>
<p>I think we want to merge the rep + invariant documents eventually</p>



<a name="152269291"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152269291" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152269291">(Dec 20 2018 at 16:45)</a>:</h4>
<p>but for discussion, niche is really where rep exploits the invariant</p>



<a name="152269300"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152269300" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152269300">(Dec 20 2018 at 16:45)</a>:</h4>
<p>if we peel off that we might as well merge the discussions</p>



<a name="152269359"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152269359" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152269359">(Dec 20 2018 at 16:46)</a>:</h4>
<p>btw, I think that enums are a clear use-case for some form of "first-field-has-zero-offset" <code>#[repr]</code></p>



<a name="152269368"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152269368" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152269368">(Dec 20 2018 at 16:46)</a>:</h4>
<p>it is silly that we use <code>#[repr(C)]</code> on all the substructs and hency fully order the fields of the variants</p>



<a name="152269373"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152269373" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152269373">(Dec 20 2018 at 16:46)</a>:</h4>
<p>when all we need is the ability to read the discriminant</p>



<a name="152269390"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152269390" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152269390">(Dec 20 2018 at 16:46)</a>:</h4>
<p>there are <em>so many</em> possible <code>repr</code></p>



<a name="152269394"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152269394" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152269394">(Dec 20 2018 at 16:46)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> Would merging rep + invariant end up putting concepts like lifetimes etc. into the rep chapter?</p>



<a name="152269401"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152269401" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152269401">(Dec 20 2018 at 16:46)</a>:</h4>
<blockquote>
<p>there are <em>so many</em> possible <code>repr</code></p>
</blockquote>
<p>I don't think this is a useful argument against any individual one</p>



<a name="152269408"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152269408" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152269408">(Dec 20 2018 at 16:47)</a>:</h4>
<p>validity invariants have nothing to do with lifetimes IMO</p>



<a name="152269443"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152269443" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152269443">(Dec 20 2018 at 16:47)</a>:</h4>
<p>but I suppose this is worth having a separate topic, and anyway it's not that imp't, the other reprs don't exist <em>now</em></p>



<a name="152269445"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152269445" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152269445">(Dec 20 2018 at 16:47)</a>:</h4>
<blockquote>
<blockquote>
<p>there are <em>so many</em> possible <code>repr</code></p>
</blockquote>
<p>I don't think this is a useful argument against any individual one</p>
</blockquote>
<p>sure, but we could instead have a "most general" one? have an attribute at each field where the user gives an offset</p>



<a name="152269456"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152269456" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152269456">(Dec 20 2018 at 16:47)</a>:</h4>
<p>that's fine</p>



<a name="152269472"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152269472" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152269472">(Dec 20 2018 at 16:47)</a>:</h4>
<p>hm I guess we'd need a DSL to compute the offset based on size+alignment of other types... yeah that'll be complicated^^</p>



<a name="152269533"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152269533" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152269533">(Dec 20 2018 at 16:48)</a>:</h4>
<blockquote>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> Would merging rep + invariant end up putting concepts like lifetimes etc. into the rep chapter?</p>
</blockquote>
<p>as far as I can see, merging them leaves nothing to be said for validity invariants</p>



<a name="152269544"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152269544" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152269544">(Dec 20 2018 at 16:48)</a>:</h4>
<p>that's why I think we shouldn't</p>



<a name="152317325"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152317325" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152317325">(Dec 21 2018 at 09:02)</a>:</h4>
<p><span class="user-mention" data-user-id="125253">@Nicole Mazzuca</span> <span class="user-mention" data-user-id="120791">@RalfJ</span> would it be possible to start linting such code ?</p>



<a name="152317348"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152317348" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152317348">(Dec 21 2018 at 09:03)</a>:</h4>
<p>which code?</p>



<a name="152318834"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152318834" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152318834">(Dec 21 2018 at 09:34)</a>:</h4>
<blockquote>
<p>packing into padding is impossible unfortunately; mem::swap would break: it overrides all bytes of both objects. so e.g. whatever &amp;mut b.a points to cannot overlap with anything packed</p>
</blockquote>



<a name="152318838"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152318838" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152318838">(Dec 21 2018 at 09:34)</a>:</h4>
<p>i wish zulip would allow replying to messages within a thread, linking the message that its being replied to (EDIT: now there is an issue for that: <a href="https://github.com/zulip/zulip/issues/11105" target="_blank" title="https://github.com/zulip/zulip/issues/11105">https://github.com/zulip/zulip/issues/11105</a>)</p>



<a name="152319509"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152319509" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152319509">(Dec 21 2018 at 09:49)</a>:</h4>
<p>absolutely zero way</p>



<a name="152334287"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152334287" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152334287">(Dec 21 2018 at 14:37)</a>:</h4>
<p>yeah no, that just breaks what references mean. I don't think it can be linted, even dynamically checking for it might be hard.</p>



<a name="152560918"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum%20repr/near/152560918" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/enum.20repr.html#152560918">(Dec 26 2018 at 16:50)</a>:</h4>
<p>Are we feeling ok about the current state of <a href="https://github.com/rust-rfcs/unsafe-code-guidelines/pull/57" target="_blank" title="https://github.com/rust-rfcs/unsafe-code-guidelines/pull/57">https://github.com/rust-rfcs/unsafe-code-guidelines/pull/57</a> ?</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>